Function Examples

Find average of two numbers

Main Function()
Step 1 : Start
Step 2 : Read a,b
Step 3 : Call the function avg and pass a,b
Step 4 : Print Avg
Step 5 : Stop
Sub Function avg(a,b)
Step 1 : Function start
Step 2 : Get the value of a,b
Step 3 : Calculate sum as
Sum=a+b
Avg=Sum/2
Step 4 : Return Avg

Identify whether the person is eligible to vote

Main Function()
Step 1 : Start
Step 2 : Call the function vote_eligible( )
Step 3 : Stop
Sub Function vote_eligible()
Step 1 : Function start
Step 2 : Read the value of age
Step 3 : Check the condition, age is greater than or equal to 18. If the condition is true goto step 3.1 else goto step 3.2
Step 3.1 : Print “You are Eligible to vote”
Step 3.2 : PrintYou are Not Eligible to vote”
Step 4: Stop

References

  • Allen B. Downey, “Think Python: How to Think Like a Computer Scientist‘‘, 2nd edition, Updated for Python 3, Shroff/O‘Reilly Publishers, 2016 (http://greenteapress.com/wp/thinkpython/)
  • Guido van Rossum and Fred L. Drake Jr, ―An Introduction to Python – Revised and updated for Python 3.2, Network Theory Ltd., 2011.
  • John V Guttag, ―Introduction to Computation and Programming Using Python‘‘, Revised and expanded Edition, MIT Press , 2013
  • Robert Sedgewick, Kevin Wayne, Robert Dondero, ―Introduction to Programming in Python: An Inter-disciplinary Approach, Pearson India Education Services Pvt. Ltd., 2016.
  • Timothy A. Budd, ―Exploring Python‖, Mc-Graw Hill Education (India) Private Ltd.,, 2015. 4. Kenneth A. Lambert, ―Fundamentals of Python: First Programs‖, CENGAGE Learning, 2012.
  • Charles Dierbach, ―Introduction to Computer Science using Python: A Computational Problem-Solving Focus, Wiley India Edition, 2013.
  • Paul Gries, Jennifer Campbell and Jason Montojo, ―Practical Programming: An Introduction to Computer Science using Python 3‖, Second edition, Pragmatic Programmers, LLC, 2013.